package org.example;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;


public class StudentGPAGUI {

    // Core system logic for managing student data and GPA calculations.
    private StudentGPASystem system;


    public StudentGPAGUI() {
        system = new StudentGPASystem();
        initUI();
    }


    private void initUI() {
        // Create the main application window (JFrame)
        JFrame frame = new JFrame("Student GPA System");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 400); // Set the window size
        frame.setLayout(new BorderLayout()); // Use a BorderLayout for the frame

        // Main Panel contains all sub-panels
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout(6, 1)); // Grid layout for organizing sub-panels

        // Panel for adding students
        JPanel addStudentPanel = new JPanel();
        addStudentPanel.setLayout(new FlowLayout());
        JLabel studentNameLabel = new JLabel("Student Name: ");
        JTextField studentNameField = new JTextField(20);
        JButton addStudentButton = new JButton("Add Student");
        addStudentPanel.add(studentNameLabel);
        addStudentPanel.add(studentNameField);
        addStudentPanel.add(addStudentButton);

        // Panel for adding grades
        JPanel addGradePanel = new JPanel();
        addGradePanel.setLayout(new FlowLayout());
        JLabel gradeStudentNameLabel = new JLabel("Student Name: ");
        JTextField gradeStudentNameField = new JTextField(10);
        JLabel courseLabel = new JLabel("Course: ");
        JTextField courseField = new JTextField(10);
        JLabel gradeLabel = new JLabel("Grade: ");
        JTextField gradeField = new JTextField(5);
        JButton addGradeButton = new JButton("Add Grade");
        addGradePanel.add(gradeStudentNameLabel);
        addGradePanel.add(gradeStudentNameField);
        addGradePanel.add(courseLabel);
        addGradePanel.add(courseField);
        addGradePanel.add(gradeLabel);
        addGradePanel.add(gradeField);
        addGradePanel.add(addGradeButton);

        // Panel for calculating GPA
        JPanel calculateGPAPanel = new JPanel();
        calculateGPAPanel.setLayout(new FlowLayout());
        JLabel calculateGPANameLabel = new JLabel("Student Name: ");
        JTextField calculateGPANameField = new JTextField(20);
        JButton calculateGPAButton = new JButton("Calculate GPA");
        JLabel gpaLabel = new JLabel("GPA: ");
        calculateGPAPanel.add(calculateGPANameLabel);
        calculateGPAPanel.add(calculateGPANameField);
        calculateGPAPanel.add(calculateGPAButton);
        calculateGPAPanel.add(gpaLabel);

        // Panel for import and export functionality
        JPanel filePanel = new JPanel();
        filePanel.setLayout(new FlowLayout());
        JButton importButton = new JButton("Import from TXT");
        JButton exportButton = new JButton("Export to TXT");
        filePanel.add(importButton);
        filePanel.add(exportButton);

        // Add sub-panels to the main panel
        mainPanel.add(addStudentPanel);
        mainPanel.add(addGradePanel);
        mainPanel.add(calculateGPAPanel);
        mainPanel.add(filePanel);

        // Add the main panel to the frame
        frame.add(mainPanel, BorderLayout.CENTER);

        // Add action listeners for buttons

        // Action listener for "Add Student" button
        addStudentButton.addActionListener(e -> {
            String name = studentNameField.getText().trim();
            if (!name.isEmpty()) {
                system.addStudent(name); // Add student to the system
                JOptionPane.showMessageDialog(frame, "Student added successfully!");
            } else {
                JOptionPane.showMessageDialog(frame, "Please enter a valid student name.");
            }
        });

        // Action listener for "Add Grade" button
        addGradeButton.addActionListener(e -> {
            String studentName = gradeStudentNameField.getText().trim();
            String course = courseField.getText().trim();
            try {
                double grade = Double.parseDouble(gradeField.getText().trim());
                system.addGrade(studentName, course, grade); // Add grade to the system
                JOptionPane.showMessageDialog(frame, "Grade added successfully!");
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(frame, "Please enter a valid grade.");
            }
        });

        // Action listener for "Calculate GPA" button
        calculateGPAButton.addActionListener(e -> {
            String studentName = calculateGPANameField.getText().trim();
            double gpa = system.getGPA(studentName); // Calculate GPA for the student
            gpaLabel.setText("GPA: " + gpa);
            JOptionPane.showMessageDialog(frame, studentName + "'s GPA: " + gpa);
        });

        // Action listener for "Import" button
        importButton.addActionListener(e -> {
            JFileChooser fileChooser = new JFileChooser();
            int returnValue = fileChooser.showOpenDialog(frame);
            if (returnValue == JFileChooser.APPROVE_OPTION) {
                File file = fileChooser.getSelectedFile();
                system.importFromTxt(file.getPath()); // Import data from file
                JOptionPane.showMessageDialog(frame, "Data imported successfully!");
            }
        });

        // Action listener for "Export" button
        exportButton.addActionListener(e -> {
            JFileChooser fileChooser = new JFileChooser();
            int returnValue = fileChooser.showSaveDialog(frame);
            if (returnValue == JFileChooser.APPROVE_OPTION) {
                File file = fileChooser.getSelectedFile();
                system.exportToTxt(file.getPath()); // Export data to file
                JOptionPane.showMessageDialog(frame, "Data exported successfully!");
            }
        });

        // Make the frame visible
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(StudentGPAGUI::new);
    }
}
